#ifndef TC_PRICECHANNEL_HPP
#define TC_PRICECHANNEL_HPP

/** \file PriceChannel.hpp
 * Definition of the PriceChannel function template.
 *
 * (C) Copyright 2003-2006 by Semih Cemiloglu
 * All rights reserved, see COPYRIGHT file for details.
 *
 * $Id: PriceChannel.hpp 110 2006-06-19 03:38:33Z semihc $
 *
 *
 */

#include "TA.hh"
#include "DBC.hpp"
#include "CircArray.hpp"



namespace TC {

  //! PriceChannel function
  TC_EXPORT template <typename InputIterator,
                      typename OutputIterator,
                      class Adaptor>
  void PriceChannel(InputIterator fromPos,
		    InputIterator fromEnd,
		    OutputIterator toPosLo,
		    OutputIterator toPosHi,
		    const unsigned period,
		    const Adaptor adaptor)
  {
    TC_PRE_CONDITION(period > 0);
    
    // Types
    typedef typename Adaptor::Price_t Price_t;

    // Circular arrays
    CircArray<Price_t> lo_array(period);
    CircArray<Price_t> hi_array(period);

    // Highest high in the previous periods
    // excluding today
    register Price_t hhigh = adaptor.hi(*fromPos);
    // Lowest low in the previous periods
    // excluding today
    register Price_t llow = adaptor.lo(*fromPos);

    // Put some meaningful values into circular arrays
    hi_array.push(hhigh);
    lo_array.push(llow);
      
    while (fromPos != fromEnd) {
      // Get highest high in the previous period
      hhigh = hi_array.max();
      // Get lowest low in the previous period
      llow = lo_array.min();
      // Write to the output
      *toPosHi++ = hhigh;
      *toPosLo++ = llow;
      // Record today's values
      hi_array.push( adaptor.hi(*fromPos) );
      lo_array.push( adaptor.lo(*fromPos) );
      // Advance the input
      ++fromPos;
    }
  }



} // end namespace


#endif /* Include guard */
